﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.POIFS;
using NPOI.Util;
using System.IO;
using Facade.Excel;

namespace PeisongWeb.admin
{
    public partial class productPriceHasChangedInCurrentDay : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CG", 1))
                {
                    parentCategoryBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            if (txtInputDt.SelectedDate != DateTime.MinValue)
            {
                where.AppendFormat("DATEDIFF(DAY,inputDt,'{0}')=0", txtInputDt.SelectedDate);
            }
            else
            {
                where.Append(" DATEDIFF(DAY,inputDt,GETDATE())=0");
            }

            if (!string.IsNullOrEmpty(ddl_CategorySub.SelectedValue) && ddl_CategorySub.SelectedValue != "-1")
            {
                where.AppendFormat(" and categoryId={0}", ddl_CategorySub.SelectedValue);
            }
            else
            {
                if (ddl_CategoryRoot.SelectedValue != "-1")
                    where.AppendFormat(" and parentId={0}",ddl_CategoryRoot.SelectedValue);
            }
            if (!string.IsNullOrEmpty(txtProductName.Text))
                where.AppendFormat(" and productName like '%{0}%'",txtProductName.Text);
            if (!string.IsNullOrEmpty(txtSellerName.Text))
                where.AppendFormat(" and companyName like '%{0}%'", txtSellerName.Text);

            DataSet ds = logic.priceSeller.selectWithColor(where.ToString(), "inputDt desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            ViewState["CacheDataTable_PPCD"] = ds.Tables[1];
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        private void parentCategoryBind()
        {
            DataTable dt = logic.category.getByParent(0);
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
            ddl_CategoryRoot.Items.Insert(0, new ListItem("-请选择-", "-1"));
        }

        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_CategoryRoot.SelectedValue != "-1")
            {
                DataTable dt = logic.category.getByParent(Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
                ddl_CategorySub.DataSource = dt;
                ddl_CategorySub.DataBind();
                ddl_CategorySub.Items.Insert(0, new ListItem("-请选择-", "-1"));
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void linkExportToExcel_Command(object sender,CommandEventArgs e)
        {
            DataTable dt = ViewState["CacheDataTable_PPCD"] as DataTable;

            DataTable newDt = dt.DefaultView.ToTable(false, new string[] { "categoryName","productName","businessNatureName","unit","companyName","oldSellerPrice","sellerPrice","inputDt"});
            newDt.Columns[0].ColumnName = "类别";
            newDt.Columns[1].ColumnName="级别";
            newDt.Columns[2].ColumnName="商品名称";
            newDt.Columns[3].ColumnName="单位";
            newDt.Columns[4].ColumnName="供应商";
            newDt.Columns[5].ColumnName="原价";
            newDt.Columns[6].ColumnName="浮动价";
            newDt.Columns[7].ColumnName = "报价时间";
            
            string title = string.Format("{0:yyyy年MM月dd日}价格变动商品清单",(txtInputDt.SelectedDate!=DateTime.MinValue?txtInputDt.SelectedDate:DateTime.Now));

            // 导出Excel
            ExportFacade exportInstance = new Facade.Excel.ExportFacade();
            Dictionary<int, FormatStyle> formatOptions = new Dictionary<int, FormatStyle>();
            exportInstance.ExportByWeb(newDt, title, title + ".xls", formatOptions, title);
        }

    }
}
